package web.servlet;

import com.alibaba.fastjson.JSONObject;
import com.rdc.example.bean.PageBean;
import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.ColumnDTO;
import com.rdc.example.bean.dto.PlayListDTO;
import com.rdc.example.bean.dto.SongDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.service.impl.ColumnServiceImpl;
import com.rdc.example.service.impl.PlayListServiceImpl;
import com.rdc.example.service.impl.SongServiceImpl;
import com.rdc.example.service.impl.UserServiceImpl;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.nio.charset.StandardCharsets;
import java.util.List;


@WebServlet("/space/*")
public class SpaceServlet extends BaseServlet {
    UserServiceImpl userService = new UserServiceImpl();
    SongServiceImpl songService = new SongServiceImpl();
    ColumnServiceImpl columnService = new ColumnServiceImpl();
    PlayListServiceImpl playListService = new PlayListServiceImpl();

    public void searchUser(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("searchUser...");
        //从前端获取数据
        String keyword = req.getParameter("keyword");
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        PageBean<UserDTO> pageBean =userService.searchUsersByUsername(keyword,currentPage,pageSize);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }

    public void searchSong(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("searchSong...");
        //从前端获取数据
        req.setCharacterEncoding("UTF-8");

        Integer type = Integer.parseInt(req.getParameter("type"));
        String keyword = req.getParameter("keyword");
        keyword = new String(keyword.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        // type  : 0->按点赞的排序   1->按创建时间的排序
        //System.out.println("type:"+type+" keyword:"+keyword+" currentPage:"+currentPage+" pageSize:"+pageSize);
        PageBean<SongDTO> pageBean = songService.searchSongsByTitleAndLikeOrCreatAtDesc(keyword,currentPage,pageSize,type);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }

    public void searchColumn(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("searchColumn...");
        //从前端获取数据
        req.setCharacterEncoding("UTF-8");
        String keyword = req.getParameter("keyword");
        keyword = new String(keyword.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        //System.out.println("type:"+type+" keyword:"+keyword+" currentPage:"+currentPage+" pageSize:"+pageSize);
        PageBean<ColumnDTO> pageBean = columnService.searchColumnsByTitle(keyword,currentPage,pageSize);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }

    public void searchPlayList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("searchPlaylist...");
        //从前端获取数据
        req.setCharacterEncoding("UTF-8");
        String keyword = req.getParameter("keyword");
        keyword = new String(keyword.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        //
        PageBean<PlayListDTO> pageBean = playListService.searchPlaylistsByTitle(keyword,currentPage,pageSize);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }

    public void randomSong(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("randomSong...");
        Result result = songService.getRandomSong();
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }

    public void randomPlaylist(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("randomPlaylist...");
        Result result = playListService.getRandomPlayList();
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }



}